package com.gitee.free.coding.core.utils;

import com.gitee.free.coding.core.enums.ErrorEnum;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    public static <T> Result<T> success(T object) {
        Result<T> result = success();
        result.data = object;
        return result;
    }

    public static <T> Result<T> success() {
        return setResult(ErrorEnum.SUCCESS);
    }

    public static <T> Result<T> fail(T object) {
        Result<T> result = fail();
        result.data = object;
        return result;
    }

    public static <T> Result<T> fail() {
        return setResult(ErrorEnum.FAIL);
    }

    public static <T> Result<T> setResult(ErrorEnum errorEnum, T object) {
        Result<T> result = setResult(errorEnum);
        result.setData(object);
        return result;
    }

    public static <T> Result<T> setResult(ErrorEnum errorEnum) {
        return setResult(errorEnum.getErrorCode(), errorEnum.getErrorMsg());
    }

    public static <T> Result<T> setResult(Integer code, String msg) {
        Result<T> result = new Result<T>();
        result.code = code;
        result.msg = msg;
        return result;
    }

}